﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace MCromwell.StaffIntranet.UI.maintenance
{
    public partial class Locations : System.Web.UI.Page
    {
        private const int FOREIGN_KEY_CONTRAINT = 547;

        protected void Page_Load(object sender, EventArgs e)
        {
            LocationDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["staffintranet"].ConnectionString;

        }

        protected void LocationDataSource_Deleted(object sender, SqlDataSourceStatusEventArgs e)
        {
            var sqlException = e.Exception as SqlException;
            
            if (sqlException == null) // bubble this up
                return;

            if (sqlException.Number == FOREIGN_KEY_CONTRAINT)
            {
                e.ExceptionHandled = true;
                grdLocations.EditIndex = -1;
                valForeignKeyConstraint.IsValid = false;
            }
        }

        protected void grdLocations_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("New", StringComparison.InvariantCultureIgnoreCase) && (Page.IsValid))
                LocationDataSource.Insert();
            
        }

        protected void LocationDataSource_Inserting(object sender, SqlDataSourceCommandEventArgs e)
        {
            var valueTextBox = grdLocations.FooterRow.FindControl("txtNewValue") as TextBox;
            var value = valueTextBox.Text;

            e.Command.Parameters.Add(new SqlParameter("@Value", value));
        }


    }
}
